using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using SumGam;
using SumGam.Globals;
using SumGam.Structs;
namespace SumGam
{
    public class Timers
    {
        public Hashtable InitList, RemoveList, ExecList;
        public class Timer
        {
            public string Name;
            public int Reps;
            public double Delay, LastExec;
            public bool IsPaused, IsInitialized;
            public Delegates.Generic Func;
            public Timer(string name, double delay, int reps, Delegates.Generic func, Double CurTime)
            {
                Name = name;
                Delay = delay;
                if (reps == 0 || reps < -1)
                {
                    reps = -1;
                }
                Reps = reps;
                Func = func;
                LastExec = CurTime;
                IsPaused = false;
                IsInitialized = false;
                Misc.TimerInitList[name] = this;
            }
            public void Pause()
            {
                IsPaused = true;
            }
            public void Resume()
            {
                IsPaused = false;
            }
            public void Off()
            {
                if (IsInitialized == true)
                {
                    Misc.TimerRemoveList[Name] = this;
                    Pause();
                }
            }
            public void Init(double CurTime)
            {
                if (IsInitialized == false)
                {
                    LastExec = CurTime;
                    Misc.TimerExecList[Name] = this;
                    IsInitialized = true;
                }
            }
            public void Exec(double CurTime)
            {
                if (IsInitialized == true)
                {
                    LastExec = CurTime;
                    if (Reps != -1)
                    {
                        Reps = Reps - 1;
                    }
                    Func();
                    if (Reps == 0)
                    {
                        this.Off();
                    }
                }
            }
        }
        public static void UpdateTimers(double CurTime)
        {
            //System.Console.WriteLine(Misc.Ticks);
            foreach (DictionaryEntry entry in Misc.TimerInitList)
            {
                Timer curtimer = (Timer)entry.Value;
                curtimer.Init(CurTime);
            }
            Misc.TimerInitList.Clear();
            foreach (DictionaryEntry entry in Misc.TimerExecList)
            {
                Timer curtimer = (Timer)entry.Value;
                if ((Misc.Time.TotalGameTime.TotalMilliseconds - curtimer.LastExec) >= curtimer.Delay && curtimer.IsPaused == false)
                {
                    curtimer.Exec(CurTime);
                }
            }
            foreach (string key in Misc.TimerRemoveList.Keys)
            {
                Misc.TimerExecList.Remove(key);
            }
            Misc.TimerRemoveList.Clear();
        }
    }
}
